package icexam.exam.print;




import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.print.PrinterJob;
import java.util.List;
import java.util.TimerTask;
import java.util.logging.Level;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
import net.sf.jasperreports.swing.JRViewer;

public class MyReportViewer extends javax.swing.JFrame {

    /** Creates new form MyReportViewer */
    public MyReportViewer(List<JasperPrint> jasperPrints) {
        initComponents();
        this.jasperPrints = jasperPrints;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(new Dimension(800, 600));
        setLocation((screenSize.width - 750) / 2, (screenSize.height - 550) / 2);
        mjr = new MyJRViewer(jasperPrints.get(0), 0, jasperPrints.size(),null);
        pnlMain.add(mjr, "Center");
        this.setVisible(true);
    }
    private MyJRViewer mjr = null;
    void printAllActionPerformed(java.awt.event.ActionEvent evt) {

        final PrinterJob pj = PrinterJob.getPrinterJob();
        final NewWaitingDialog dlg = new NewWaitingDialog();
        dlg.setAlwaysOnTop(true);
        dlg.showUp("Loading printer ....", new TimerTask() {

            public void run() {
                dlg.unShow();
                pj.printDialog();
                JRPrintServiceExporter exporter = new JRPrintServiceExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
                PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();

                try {
                    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, pj.getPrintService());
                    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
                    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);
                    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
                    exporter.exportReport();
                } catch (JRException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        });

    }

    void previousActionPerformed(java.awt.event.ActionEvent evt) {

        if(!mjr.isBeginOfPage())
            mjr.setPageIndex(mjr.getPageIndex()-1);
        else{
            if (index > 0) {

                index--;
                pnlMain.removeAll();
                mjr = new MyJRViewer(jasperPrints.get(index), index, jasperPrints.size(),mjr.getZoomRatio());
                mjr.setPageIndex(mjr.getPageCount()-1);
                pnlMain.add(mjr, "Center");
                pnlMain.validate();
            }
        }
    }

    void nextActionPerformed(java.awt.event.ActionEvent evt) {

        if(!mjr.isEndOfPage())
            mjr.setPageIndex(mjr.getPageIndex()+1);
        else{            
            if (index < jasperPrints.size() - 1) {

                index++;
                pnlMain.removeAll();
                mjr = new MyJRViewer(jasperPrints.get(index), index, jasperPrints.size(),mjr.getZoomRatio());
                pnlMain.add(mjr, "Center");
                pnlMain.validate();                
            }
        }
    }
    private int index = 0;
    private List<JasperPrint> jasperPrints;

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlMain = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        exitBut = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Print Preview");

        pnlMain.setPreferredSize(new java.awt.Dimension(800, 600));

        javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
        pnlMain.setLayout(pnlMainLayout);
        pnlMainLayout.setHorizontalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 643, Short.MAX_VALUE)
        );
        pnlMainLayout.setVerticalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 360, Short.MAX_VALUE)
        );

        exitBut.setText("退出");
        exitBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(536, Short.MAX_VALUE)
                .addComponent(exitBut, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(exitBut)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlMain, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pnlMain.setLayout(new BorderLayout());

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButActionPerformed
       this.dispose();
}//GEN-LAST:event_exitButActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton exitBut;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pnlMain;
    // End of variables declaration//GEN-END:variables

    class MyJRViewer extends JRViewer {

        private JButton btnPrintAll;
        private JButton btnPrevious;
        private JButton btnNext;
//        private JPanel pnlSep01;
        private JPanel pnlSep02;
        private int partIndex;
        private int partCount;
        public MyJRViewer(JasperPrint jp, int partIndex, int partCount,Float newZoom) {
            
            super(jp);
            this.partCount = partCount;
            this.partIndex = partIndex;
            tlbToolBar.remove(0);
            tlbToolBar.remove(1);
            tlbToolBar.remove(0);
            tlbToolBar.remove(0);
            tlbToolBar.remove(0);
            tlbToolBar.remove(0);
            tlbToolBar.remove(0);
            tlbToolBar.remove(0);
            tlbToolBar.remove(0);
            tlbToolBar.remove(0);
            this.lblStatus.setText("Page "+(1+this.getPageCount()*this.partIndex)+" of "+(this.partCount*this.viewerContext.getPageCount()));
            this.pnlSep02 = new JPanel();
            this.pnlSep02.setMaximumSize(new Dimension(10, 10));
            this.btnPrintAll = new JButton();
            this.btnPrintAll.setText("打印全部");
            this.btnPrintAll.setMargin(new Insets(2, 2, 2, 2));
            this.btnPrintAll.setMaximumSize(new Dimension(60, 23));
            this.btnPrintAll.setMinimumSize(new Dimension(60, 23));
            this.btnPrintAll.setPreferredSize(new Dimension(60, 23));
            this.btnPrintAll.addActionListener(
                    new java.awt.event.ActionListener() {

                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            printAllActionPerformed(evt);
                        }
                    });
            this.tlbToolBar.add(btnPrintAll);
            tlbToolBar.add(pnlSep02);
            this.btnPrevious = new JButton();
            this.btnPrevious.setText("前一页");
            this.btnPrevious.setMargin(new Insets(2, 2, 2, 2));
            this.btnPrevious.setMaximumSize(new Dimension(60, 23));
            this.btnPrevious.setMinimumSize(new Dimension(60, 23));
            this.btnPrevious.setPreferredSize(new Dimension(60, 23));
            this.btnPrevious.addActionListener(
                    new java.awt.event.ActionListener() {

                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            previousActionPerformed(evt);
                        }
                    });
            tlbToolBar.add(btnPrevious);
            this.btnNext = new JButton();
            this.btnNext.setText("下一页");
            this.btnNext.setMargin(new Insets(2, 2, 2, 2));
            this.btnNext.setMaximumSize(new Dimension(60, 23));
            this.btnNext.setMinimumSize(new Dimension(60, 23));
            this.btnNext.setPreferredSize(new Dimension(60, 23));
            this.btnNext.addActionListener(
                    new java.awt.event.ActionListener() {

                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            nextActionPerformed(evt);
                        }
                    });
            tlbToolBar.add(btnNext);
            if(newZoom!=null)
                this.viewerContext.setZoomRatio(newZoom);
            else{
                setZoomRatio((float) 0.5763);
                this.btnPrevious.setEnabled(false);
            }
            if(partIndex+1==partCount&&this.viewerContext.getPageIndex()+1==this.viewerContext.getPageCount())
                this.btnNext.setEnabled(false);
        }

        private MyJRViewer(JasperPrint jp, int partIndex, int partCount, float zoomRatio, int i) {
            this(jp, partIndex, partCount,zoomRatio);


        }

        public void setPageIndex(int index){
            this.viewerContext.setPageIndex(index);
            this.viewerContext.refreshPage();
            this.viewerContext.getPageCount();
            this.lblStatus.setText("Page "+(index+1+this.getPageCount()*this.partIndex)+" of "+(this.partCount*this.viewerContext.getPageCount()));
            if(partIndex+1==partCount&&this.viewerContext.getPageIndex()+1==this.viewerContext.getPageCount())
                this.btnNext.setEnabled(false);
            else
                this.btnNext.setEnabled(true);
            if(partIndex==0&&this.viewerContext.getPageIndex()==0)
                this.btnPrevious.setEnabled(false);
            else
                this.btnPrevious.setEnabled(true);

        }
        public int getPageCount(){
            return this.viewerContext.getPageCount();
        }
        public boolean isEndOfPage(){
            return this.viewerContext.getPageIndex()+1==this.viewerContext.getPageCount();
        }
        public boolean isBeginOfPage(){
            return this.viewerContext.getPageIndex()==0;
        }
        public float getZoomRatio(){
            return this.viewerContext.getZoom();
        }
        public void makeEnableNextBut(boolean b){
            this.btnNext.setEnabled(b);
        }
    }
}
